package com.qiangesoft.blog.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.blog.common.constants.FieldConstants;
import com.qiangesoft.blog.common.mvc.PageQuery;
import com.qiangesoft.blog.common.mvc.PageResultVO;
import com.qiangesoft.blog.common.utils.HumpLineUtils;
import com.qiangesoft.blog.system.entity.SysDict;
import com.qiangesoft.blog.system.entity.SysDictData;
import com.qiangesoft.blog.system.mapper.SysDictMapper;
import com.qiangesoft.blog.system.service.ISysDictDataService;
import com.qiangesoft.blog.system.service.ISysDictService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

import static com.qiangesoft.blog.common.constants.FieldConstants.LIMIT_ONE;

/**
 * <p>
 * 字典 服务实现类
 * </p>
 *
 * @author qiange
 * @since 2023-06-01
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements ISysDictService {

    private final ISysDictDataService sysDictDataService;

    @Override
    public PageResultVO<SysDict> listDict(PageQuery pageQuery, String name, Boolean isPublish, String descColumn, String ascColumn) {
        PageResultVO<SysDict> pageResultVO = new PageResultVO<>();
        Integer pageNum = pageQuery.getPageNum();
        Integer pageSize = pageQuery.getPageSize();
        pageResultVO.setPageNum(pageNum);
        pageResultVO.setPageSize(pageSize);

        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<SysDict>()
                .eq(isPublish != null, FieldConstants.IS_PUBLISH, isPublish)
                .like(StringUtils.isNotBlank(name), FieldConstants.NAME, name);
        if (StringUtils.isNotEmpty(ascColumn)) {
            // 将驼峰转换成下划线
            String column = HumpLineUtils.humpToLine2(ascColumn);
            queryWrapper.orderByAsc(StringUtils.isNotEmpty(ascColumn), column);
        } else if (StringUtils.isNotEmpty(descColumn)) {
            // 将驼峰转换成下划线
            String column = HumpLineUtils.humpToLine2(descColumn);
            queryWrapper.orderByDesc(column);
        } else {
            queryWrapper.orderByDesc(FieldConstants.SORT, FieldConstants.CREATE_TIME);
        }
        Page<SysDict> dictPage = baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        pageResultVO.setTotal(dictPage.getTotal());
        pageResultVO.setPages(dictPage.getPages());
        pageResultVO.setResults(dictPage.getRecords());
        return pageResultVO;
    }

    @Override
    public void insertDict(SysDict dict) {
        validateType(dict.getType());
        baseMapper.insert(dict);
    }

    @Override
    public void updateDict(SysDict dict) {
        SysDict temp = baseMapper.selectById(dict.getId());
        if (!temp.getType().equals(dict.getType())) validateType(dict.getType());
        baseMapper.updateById(dict);
    }

    @Override
    public void deleteDict(Long id) {
        int count = sysDictDataService.count(new QueryWrapper<SysDictData>().eq(FieldConstants.DICT_TYPE_ID, id));
        Assert.isTrue(count == 0, "该字典类型存在字典数据!");
        baseMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        int count = sysDictDataService.count(new QueryWrapper<SysDictData>().in(FieldConstants.DICT_TYPE_ID, ids));
        Assert.isTrue(count == 0, "所选字典类型中存在字典数据!");
        baseMapper.deleteBatchIds(ids);
    }


    /**
     * 校验存在性
     *
     * @param type
     */
    public void validateType(String type) {
        SysDict temp = baseMapper.selectOne(new QueryWrapper<SysDict>().eq(FieldConstants.TYPE, type).last(LIMIT_ONE));
        Assert.isNull(temp, "该字典类型已存在!");
    }
}
